package cn.iocoder.yudao.module.erp.controller.admin.accountplanrecord;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.erp.controller.admin.accountplanrecord.vo.*;
import cn.iocoder.yudao.module.erp.dal.dataobject.accountplanrecord.AccountPlanRecordDO;
import cn.iocoder.yudao.module.erp.service.accountplanrecord.AccountPlanRecordService;

@Tag(name = "管理后台 - 客户余额对应记录")
@RestController
@RequestMapping("/erp/account-plan-record")
@Validated
public class AccountPlanRecordController {

    @Resource
    private AccountPlanRecordService accountPlanRecordService;

    @PostMapping("/create")
    @Operation(summary = "创建客户余额对应记录")
    @PreAuthorize("@ss.hasPermission('erp:account-plan-record:create')")
    public CommonResult<Long> createAccountPlanRecord(@Valid @RequestBody AccountPlanRecordSaveReqVO createReqVO) {
        return success(accountPlanRecordService.createAccountPlanRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新客户余额对应记录")
    @PreAuthorize("@ss.hasPermission('erp:account-plan-record:update')")
    public CommonResult<Boolean> updateAccountPlanRecord(@Valid @RequestBody AccountPlanRecordSaveReqVO updateReqVO) {
        accountPlanRecordService.updateAccountPlanRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除客户余额对应记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:account-plan-record:delete')")
    public CommonResult<Boolean> deleteAccountPlanRecord(@RequestParam("id") Long id) {
        accountPlanRecordService.deleteAccountPlanRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得客户余额对应记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:account-plan-record:query')")
    public CommonResult<AccountPlanRecordRespVO> getAccountPlanRecord(@RequestParam("id") Long id) {
        AccountPlanRecordDO accountPlanRecord = accountPlanRecordService.getAccountPlanRecord(id);
        return success(BeanUtils.toBean(accountPlanRecord, AccountPlanRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得客户余额对应记录分页")
    @PreAuthorize("@ss.hasPermission('erp:account-plan-record:query')")
    public CommonResult<PageResult<AccountPlanRecordRespVO>> getAccountPlanRecordPage(@Valid AccountPlanRecordPageReqVO pageReqVO) {
        PageResult<AccountPlanRecordDO> pageResult = accountPlanRecordService.getAccountPlanRecordPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, AccountPlanRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出客户余额对应记录 Excel")
    @PreAuthorize("@ss.hasPermission('erp:account-plan-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportAccountPlanRecordExcel(@Valid AccountPlanRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<AccountPlanRecordDO> list = accountPlanRecordService.getAccountPlanRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "客户余额对应记录.xls", "数据", AccountPlanRecordRespVO.class,
                        BeanUtils.toBean(list, AccountPlanRecordRespVO.class));
    }

}